package system.student.login.impl;

import system.student.entity.login.LoginRequest;
import system.student.entity.login.LoginResponse;
import system.student.entity.login.LoginType;
import system.student.entity.user.UserInfo;
import system.student.login.LoginStrategy;
import system.student.mapper.UserMapper;
import system.student.util.MapperUtil;
import system.student.util.SmsCodeUtil;

import java.util.Scanner;


public class PhoneLoginStrategy implements LoginStrategy {
    private static final UserMapper userMapper = MapperUtil.getMapper(UserMapper.class);
    @Override
    public LoginResponse login(LoginRequest loginRequest) {
        String phone = loginRequest.getPhone();

        UserInfo user = userMapper.loginByPhone(phone);
        //TODO 验证码登录

        // 用户不存在
        if (user == null) {
            System.out.println("用户不存在");
            return null;
        }

        String smsCode = SmsCodeUtil.createSmsCode();
        System.err.println("验证码为: " + smsCode);
        System.out.println("请输入验证码");
        Scanner scanner = new Scanner(System.in);
        String code = scanner.nextLine();
        if (!code.equals(smsCode)) {
            System.out.println("验证码错误");
            return null;
        }


        //查看用户状态
        if (user.getState() == 0){
            System.out.println("账号被锁定");
            return null;
        }
        return system.student.login.impl.LoginStrategy.getLoginResponse(user);
    }

    @Override
    public LoginType getLoginType() {
        return LoginType.Phone;
    }
}
